﻿/*****************************************************************//**
 * @file    FITKGraphObjectPredefinedFieldBase.h
 * @brief   Graph object base for predefined field.
 *
 * @author  ChengHaotian (yeguangbaozi@foxmail.com)
 * @date    2024-07-04
 *********************************************************************/

#ifndef __FITKGRAPHOBJECTPREDEFINEDFIELDBASE_H__
#define __FITKGRAPHOBJECTPREDEFINEDFIELDBASE_H__

#include "FITKGraphObjectMarkBase.h"

#include "FITKVTKGraphAdaptorAPI.h"

 // Forward declaration
class vtkPoints;
class vtkCellArray;
class vtkPolyData;
class vtkUnstructuredGrid;
class vtkGlyph3D;
class vtkDistanceToCamera;
class vtkRenderer;
class vtkSelectionNode;
class vtkSelection;
class vtkExtractSelection;
class vtkIdTypeArray;
class vtkIntArray;
class vtkCell;
class vtkPointSource;

class FITKGraphActor;
class FITKPolygonSource;

namespace Interface
{
    class FITKAbaAbstractPredefinedField;
    class FITKBoundaryLocation;
    class FITKModelSet;
}

namespace Exchange
{
    /**
     * @brief   Graph object base for predefined field.
     * @author  ChengHaotian (yeguangbaozi@foxmail.com)
     * @date    2024-07-04
     */
    class FITKVTKGRAPHADAPTORAPI FITKGraphObjectPredefinedFieldBase : public FITKGraphObjectMarkBase
    {
        // Regist
        FITKGraphObjectRegist(FITKGraphObjectPredefinedFieldBase, FITKGraphObjectMarkBase);
        FITKCLASS(Exchange, FITKGraphObjectPredefinedFieldBase);

    public:
        /**
         * @brief   Constructor.
         * @param   caseData: The case data
         * @param   fieldData: The predefined field data object
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        FITKGraphObjectPredefinedFieldBase(AbaqusData::FITKDataCase* caseData, Interface::FITKAbaAbstractPredefinedField* fieldData);

        /**
         * @brief   Destructor.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual ~FITKGraphObjectPredefinedFieldBase();

        /**
         * @brief   Show or hide graph object.[virtual][override]
         * @param   visibility: The visibility of graph object
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void setVisible(bool visibility) override;
    
        /**
         * @brief   Highlight the predefined field graph actors.[virtual][override]
         * @param   color: The given color for highlighting this time[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void highlight(QColor color = QColor()) override;

        /**
         * @brief   Dis-highlight the predefined field graph actors.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void disHighlight() override;

        /**
         * @brief   Update all graph actors.[virtual][override]
         * @param   forceUpdate: Force update[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void update(bool forceUpdate = false) override;

        /**
         * @brief   Update the visibility with data object's visibility.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void updateVisibility() override;

        /**
         * @brief   Set the render, which will add this graph object to itself.( for calculating size )[override]
         * @param   render: The FITKGrapgRender
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        void setFITKRender(Comp::FITKGraphRender* render) override;

        /**
         * @brief   Simplified the graph object if necessary.[virtual][override]
         * @param   enabled: Enabled simplified flag
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void simplified(bool enabled) override;

        /**
         * @brief   If the graph object has relation with the given data object id.[virtual][override]
         * @param   dataObjId: The data object id
         * @return  Has relation
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool isRelatedTo(int dataObjId) override;

        /**
         * @brief   Get the fixed sized bounds of this graph object.[virtual][override]
         * @param   The bounds size
         * @return  If the graph object has fixed sized bounds
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool getFixedBounds(double* bounds) override;

    protected:
        /**
         * @brief   Constructor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        FITKGraphObjectPredefinedFieldBase() = default;

        /**
         * @brief   Initialize.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void init();

        /**
         * @brief   Initialize the actors.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void initActors();

        /**
         * @brief   Clear the model ids and the visibility hash.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void clearModelsInfo() override;

        /**
         * @brief   Clear the data.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void clearData() override;

        /**
         * @brief   Check if the data has been changed.[virtual]
         * @return  Is data changed
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool dataChanged();

        /**
         * @brief   Generate the poly data for the predefined field.[virtual]
         * @param   fieldData: The predefined field data object
         * @return  Is OK
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool generatePolyData(Interface::FITKAbaAbstractPredefinedField* fieldData);

        /**
         * @brief   Generate the poly data for the location.[virtual]
         * @param   loc: The constraint data location object
         * @param   modelIds: The locations' model ids( For updating visibility. )
         * @param   ugrid: The location grid data
         * @return  Is OK
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool generateLocPolyData(Interface::FITKBoundaryLocation* loc, QList<int>& modelIds, vtkUnstructuredGrid* ugrid);

        /**
         * @brief   Generate predefined field actor.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual void generateGraph() override;

        /**
         * @brief   Get the visibility from the data object.[virtual][override]
         * @return  The visibility
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        virtual bool getDataVisibility() override;

        /**
         * @brief   Generate set or comp-set or surface grid data.
         * @param   setSurfGrid: The set or surface grid data
         * @param   loc: The location data
         * @param   modelIds: The set's model ids[quote]
         * @param   subSetIds: The sub set ids[quote]
         * @param   removeNormals: Need to remove the normals array[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        void generateComponentGridData(vtkUnstructuredGrid* setSurfGrid, Interface::FITKBoundaryLocation* loc, QList<int>& modelIds, QList<int>& subSetIds, bool removeNormals = true);

    protected:
        /**
         * @brief   The glyph list.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        QList<vtkGlyph3D*> m_glyphList;

        /**
         * @brief   Fixed size filters
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        QList<vtkDistanceToCamera*> m_distToCameraList;

        /**
         * @brief   The default resolution of input arrow source.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        int m_sourceResolution = 6;

        /**
         * @brief   The glyph size of the shape.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        double m_glyphSize = 10.;

        /**
         * @brief   The glyph size of the shape while the graph object is highlighting.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        double m_highlightGlyphSize = 60.;

        /**
         * @brief   The graph actor for predefined field glyphs.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        FITKGraphActor* m_fActor{ nullptr };

        /**
         * @brief   The points data for predefined field lines.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkPoints* m_pointsLoc{ nullptr };

        /**
         * @brief   The grid data for predefined field's location.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkUnstructuredGrid* m_gridLoc{ nullptr };

        /**
         * @brief   The locations's model id data array.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkIntArray* m_modelIdArrayLoc{ nullptr };

        /**
         * @brief   The points data for predefined field lines.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkPoints* m_points{ nullptr };

        /**
         * @brief   The grid data for predefined field's glyph.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkUnstructuredGrid* m_ugrid{ nullptr };

        /**
         * @brief   The model id data array.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkIntArray* m_modelIdArray{ nullptr };

        /**
         * @brief   The location's set or surface id.( Set or surface )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        int m_locationCompId = -1;

        /**
         * @brief   The sub set ids of the comp set or surface.( the location of the field )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-18
         */
        QList<int> m_subSetIds;

        /**
         * @brief   The selection node for extract mesh grid.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkSelectionNode* m_selectNode{ nullptr };

        /**
         * @brief   The selection for vtkSelectionNode.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkSelection* m_section{ nullptr };

        /**
         * @brief   The mesh grid extractor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkExtractSelection* m_extractSelection{ nullptr };

        /**
         * @brief   The indice array of extract id.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        vtkIdTypeArray* m_idArray{ nullptr };

        /**
         * @brief   All model ids for surface of the location.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        QList<int> m_modelIds;

        /**
         * @brief   The visibility of all locations' parent models.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-04
         */
        QHash<int, bool> m_modelVisibilityHash;

    };
}   // namespace Exchange

#endif // __FITKGRAPHOBJECTPREDEFINEDFIELDBASE_H__
